import Model from './abstract';
import ModelCatalogCategory from './catalog/category';
import ModelCatalogProduct from './catalog/product';
import ModelUserGroup from './user/group';
import ModelUser from './user';
import ModelStore from './store';

export default class ModelVersion extends Model {

      constructor() {
            super();
            this.modelCategory = new ModelCatalogCategory();
            this.modelProduct = new ModelCatalogProduct();
            this.modelUserGroup = new ModelUserGroup();
            this.modelUser = new ModelUser();
            this.modelStore = new ModelStore();
      }

      /**
       * 数据下载
       */
      async download(data, callback) {
            if (typeof(data) == 'object') {
                  let keys = Object.keys(data);
                  for (key of keys) {
                        let rows = data[key];
                        key = key.toLowerCase();
                        switch (key) {
                              case 'category': 
                                    if(await this.modelCategory.download(rows)) {
                                          callback(key);
                                    }
                                    break;
                              case 'product': 
                                    if(await this.modelProduct.download(rows)) {
                                          callback(key);
                                    } 
                                    break;
                              case 'user_group': 
                                    if(await this.modelUserGroup.download(rows)) {
                                          callback(key);
                                    } 
                                    break;
                              case 'user': 
                                    if(await this.modelUser.download(rows)) {
                                          callback(key);
                                    }
                                    break;
                              case 'store': 
                                    if(await this.modelStore.download(rows)) {
                                          callback(key);
                                    }
                                    break;
                        }
                  }
                  callback(1);
                  return true;
            }
            callback(0);
            return false;
      }

      /**
       * 数据上传
       */
      async upload(callback) {
            if(await this.modelCategory.upload()) {
                  callback("category");
            }
            if(await this.modelProduct.upload()) {
                  callback("product");
            }
            if(await this.modelUserGroup.upload()) {
                  callback("user_group");
            }
            if(await this.modelUser.upload()) {
                  callback("user");
            }
            if(await this.modelStore.upload()) {
                  callback("store");
            }
            return true;
      }

      /**
       * app版本升级接口
       */
      async upgrade() {
      }

}